package com.xiaomi.baidumap;

import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xiaomi.baidumap.common.ActivityUtils;
import com.xiaomi.baidumap.common.LogUtils;
import com.xiaomi.baidumap.favorite.FavoriteFragment;
import com.xiaomi.baidumap.gank.model.GankFragment;
import com.xiaomi.baidumap.github.HotRepoFragment;

import com.xiaomi.baidumap.hotuser.HotUserFragment;
import com.xiaomi.baidumap.model.UserRepo;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

//    private DrawerLayout drawerLayout;
//    private NavigationView navigationView;
    private ActivityUtils activityUtils;
//    private Toolbar toolbar;

    @BindView(R.id.toolbar) Toolbar toolbar;
    @BindView(R.id.drawerLayout) DrawerLayout drawerLayout; // 抽屉(包含内容+侧滑菜单)
    @BindView(R.id.navigationView) NavigationView navigationView; // 侧滑菜单视图

    // 热门仓库Fragment
    private HotRepoFragment hotRepoFragment;
    private Button btnLogin;
    private ImageView ivIcon;
    private HotUserFragment hotUserFragment;
    private FavoriteFragment favoriteFragment;
    private GankFragment gankFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawerlayout_main);

        ButterKnife.bind(this);


        //设置toolbar显示
        setSupportActionBar(toolbar);
        activityUtils = new ActivityUtils(this);
        navigationView.setNavigationItemSelectedListener(this);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
        toggle.syncState();
        drawerLayout.setDrawerListener(toggle);


        //得到NavigationView里面的头布局的控件
        btnLogin = ButterKnife.findById(navigationView.getHeaderView(0), R.id.btnLogin);
        ivIcon = ButterKnife.findById(navigationView.getHeaderView(0), R.id.ivIcon);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activityUtils.startActivity(com.xiaomi.baidumap.login.LoginActivity.class);
                finish();
            }
        });

        // 默认显示的是热门仓库fragment
        hotRepoFragment = new HotRepoFragment();
        replaceFragment(hotRepoFragment);

       // hotUserFragment = new HotUserFragment();
     //   replaceFragment(hotUserFragment);
    }

    //主要做了我们基本登录信息的改变
    @Override protected void onStart() {
        super.onStart();
        if (UserRepo.isEmpty()){
            btnLogin.setText(R.string.login_github);
            return;
        }
        btnLogin.setText(R.string.switch_account);
        LogUtils.d(UserRepo.getUser().getLogin()+"\\\\");
        //设置Main页面的标题
        getSupportActionBar().setTitle(UserRepo.getUser().getLogin());
        //设置我们的用户头像
//        Picasso.with(this).load(UserRepo.getUser().getAvatar()).into(ivIcon);
        ImageLoader.getInstance().displayImage(UserRepo.getUser().getAvatar(), ivIcon);
    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.container, fragment);
        transaction.commit();
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        if(item.isChecked()){
            item.setCheckable(false);
        }
        switch (item.getItemId()){
            //热门仓库
            case R.id.github_hot_repo:
                if (!hotRepoFragment.isAdded()) {
                    replaceFragment(hotRepoFragment);
                }
                break;
            //开发者
            case R.id.github_hot_coder:
                if (hotUserFragment == null){
                    hotUserFragment = new HotUserFragment();
                }
                if (!hotUserFragment.isAdded()){
                    replaceFragment(hotUserFragment);
                }
                break;
            //收藏
            case R.id.arsenal_my_repo:
                if (favoriteFragment == null){
                    favoriteFragment  = new FavoriteFragment();
                }
                if (!favoriteFragment .isAdded()){
                    replaceFragment(favoriteFragment );
                }
                break;
            //每日干货
            case R.id.tips_daily:
                if (gankFragment == null){
                    gankFragment = new GankFragment();
                }
                if (!gankFragment.isAdded()){
                    replaceFragment(gankFragment);
                }
                break;
        }
        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override public void onBackPressed() {
        super.onBackPressed();
        if (drawerLayout.isDrawerOpen(GravityCompat.START)){
            drawerLayout.closeDrawer(GravityCompat.START);
        }
    }

}
